import { defineStore } from 'pinia'
import { employeeAll, employeeCheck } from '../../api/employee'

// 用户模块 token setToken removeToken
export const useEmployeeStore = defineStore(
  'employee',
  {
    state: () => ({
      //所有员工信息
      employees: {
        employee: [],
        total: 3
      },
      //所有员工信息--用于分页
      allEmployees: [],
      //模糊查询需要的数据
      search: {
        department: '',
        name: ''
      },
      //部门分类
      departments: [],
      // 职位分类
      positions: []
    }),
    actions: {
      async fetchEmployeeAll() {
        const res = await employeeAll()
        Object.assign(this.employees, res.data) //整体赋值
        this.allEmployees = res.data.employee
        this.departments = new Set(
          this.employees.employee.map((emp) => emp.department)
        )
        this.positions = new Set(
          this.employees.employee.map((emp) => emp.position)
        )
      },
      async fetchEmployeeSearch(search) {
        const res = await employeeCheck(search)

        this.allEmployees = res.data.employee
        this.employees.total = res.data.total
      }
    }
  },
  {
    persist: true
  }
)
